﻿using System.IO;
using System.Text;

namespace Transforms.Core
{
    public interface IDataSource
    {
        byte[] GetData();

        long Length { get; }
    }

    public class FileSourceItem : IDataSource
    {
        private readonly string _fileName;
        private readonly long _length;

        public string FileName { get { return _fileName; } }

        public FileSourceItem(string fileName)
        {
            _fileName = fileName;
            _length = new FileInfo(fileName).Length;
        }

        public byte[] GetData()
        {
            return File.ReadAllBytes(FileName);
        }

        public override string ToString()
        {
            return string.Format("file: [{0}]", FileName);
        }

        public long Length
        {
            get { return _length; }
        }
    }

    public class TextSourceItem : IDataSource
    {
        private const int MaxShortSize = 10;

        private readonly byte[] _data;
        private readonly string _shortText;

        public TextSourceItem(string text)
        {
            _shortText = text.Length <= MaxShortSize ? text : text.Substring(0, MaxShortSize) + "...";
            _data = Encoding.Default.GetBytes(text);
        }

        public byte[] GetData()
        {
            return _data;
        }

        public override string ToString()
        {
            return string.Format("text: [{0}]", _shortText);
        }

        public long Length
        {
            get { return _data.Length; }
        }
    }
}
